<template>
  <div>
    <van-nav-bar title="黑马头条-登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="fromLogin.mobile"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[{
          required: true,
          message: '请填写用户名' ,
          trigger:'onChange',
          pattern:/^1[3-9]\d{9}$/
          }]"
      />
      <van-field
        v-model="fromLogin.code"
        type="password"
        label="密码"
        required
        placeholder="请输入密码"
        :rules="[{
          required: true,
          message: '请填写密码' ,
          trigger:'onChange',
          pattern:/^\d{6}$/
          }]"
      />
      <div style="margin: 16px;">
        <van-button
          round block
          type="info"
          native-type="submit"
          :loading="blo"
          :disabled="blo"
          loading-text="加载中..."
         >登陆</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { Notify } from 'vant'
import { mapMutations } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      blo: false,
      fromLogin: {
        mobile: '13245637594',
        code: '246810'
      }
    }
  },
  methods: {
    ...mapMutations(['setToken', 'setRefreshToken']),
    async onSubmit () {
      try {
        this.blo = true
        const res = await loginAPI(this.fromLogin)
        Notify({ type: 'success', message: '登陆成功^v^' })
        this.setToken(res.data.data.token)
        this.setRefreshToken(res.data.data.refresh_token)
        this.$router.replace({
          path: '/layout'
        })
        this.blo = false
      } catch (err) {
        Notify({ type: 'danger', message: '登陆失败TwT' })
        this.blo = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
  // .van-nav-bar {
  //   background: #007bff;
  // }

  // /* /deep/ 就是把data-v-hash值选择器写到类名的前面 */
  // /deep/ .van-nav-bar__title {
  //   color: white;
  // }
</style>
